var SearchView = Backbone.View.extend({
    initialize: function (spec) {
		this.geocoder = spec.geocoder;
		this.dishCollectionView = spec.dishCollectionView;
    },
	
	events : {
		"submit" : "searchSubmit"
		// "click .submit_button" : "searchSubmit",
		// "keypress #id_query" : "searchSubmit"
	},

	searchSubmit : function () { 
		// $("#search-results").html("test");
		// return false;
		this.currentQuery = $("#id_query").val();
		this.currAddress = $("#id_location").val();
		
		var currentQuery = this.currentQuery;
		var currAddress = this.currAddress;
		var dishCollectionView = this.dishCollectionView;
		var dishCollection = this.model.dishes;
		try {
		this.geocoder.geocode( { 'address': currAddress }, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				var lat_string = "&lat=" +results[0].geometry.location.lat();
				var lng_string = "&lng=" + results[0].geometry.location.lng();
				var paramStrng = "query=" + encodeURIComponent(currentQuery) + lat_string + lng_string;
				if (dishCollectionView.map != null) {
					dishCollectionView.map.setCenter(results[0].geometry.location);
					// for testing purposes only
					//$('#geo_results').text(results[0].geometry.location.toString());
					//$('#search_query').text(currentQuery);
					//$('#search_location').text(currAddress);
					dishCollection.url = "/search/?ajax&" + paramStrng;
					dishCollection.removeAll();
					dishCollection.fetch();
					// $("#search_results").load("/search/?ajax&" + paramStrng, 
						// function() {
							// // do something when everything is done loading...
						// });
				}
				else {
					window.location = "/search/?" + paramStrng + "&location=" + currAddress;
				}
			} else {
				alert("Geocode was not successful for the following reason: " + status);
			}
		});
		}
		catch (err)
		{
			alert(err);
		}
		return false;
	}
});
